package com.yqz.common.validator;

import com.yqz.common.CodeMsg;
import com.yqz.common.exception.BussiException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

public class ValidatorUtil {

    private static final Validator validator;

    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }

    public  static  void validate(Object form){
        Set<ConstraintViolation<Object>> msgs = validator.validate(form);
        for (ConstraintViolation<Object> msg : msgs) {
            String message = msg.getMessage();
            throw new BussiException(CodeMsg.PARAM_VALIDATE_ERROR.CODE,CodeMsg.PARAM_VALIDATE_ERROR.MSG + message);
        }
    }

}
